﻿using AutoMapper;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.Traceability.Domain;
using B.S.Traceability.Infrastructure.Interface;
using B.S.Traceability.Write.API.Applicantion.Command.SlaughterInfoCommand;
using MediatR;

namespace B.S.Traceability.Write.API.Applicantion.CommandHandler.SlaughterInfoCommandHandler
{
    public class AddSlaughterInfoCommandHandler:IRequestHandler<AddSlaughterInfoCommand,APIResult<int>>
    {
        private readonly IBaseRepository<SU_SlaughterInfo> slauRep;
        private readonly IMapper mapper;

        public AddSlaughterInfoCommandHandler(IBaseRepository<SU_SlaughterInfo> slauRep, IMapper mapper)
        {
            this.slauRep = slauRep;
            this.mapper = mapper;
        }
        public Task<APIResult<int>> Handle(AddSlaughterInfoCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result = new APIResult<int>();
            var model = mapper.Map<SU_SlaughterInfo>(request);
            model.ProductionCode = request.ProductionCode;
            var res = slauRep.Add(model).Result;
            if (res > 0)
            {
                result.Code = APIEnum.添加成功;
                result.Msg = APIEnum.添加成功.ToString();
                result.data = res;
            }
            else
            {
                result.Code = APIEnum.添加失败;
                result.Msg = APIEnum.添加失败.ToString();
                result.data = res;
            }
            return Task.FromResult(result);
        }

    }
}
